package com.zhf.cloud.gateway.springcloudgateway.controller;

import com.zhf.cloud.gateway.springcloudgateway.base.BaseResponse;
import com.zhf.cloud.gateway.springcloudgateway.base.PageEntity;
import com.zhf.cloud.gateway.springcloudgateway.entity.LogRecord;
import com.zhf.cloud.gateway.springcloudgateway.entity.LoginRecord;
import com.zhf.cloud.gateway.springcloudgateway.logback.mongodb.MongoDBRead;
import com.zhf.cloud.gateway.springcloudgateway.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author: 曾鸿发
 * @create: 2021-11-22 12:35
 * @description：
 **/
@Slf4j
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    LogService logService;

    @Autowired
    MongoDBRead mongoDBRead;

    @PostMapping("/getLoginRecord")
    public BaseResponse getLoginRecord(@RequestBody PageEntity<LoginRecord> pageEntity) {
        BaseResponse baseResponse = new BaseResponse();
        if(null == pageEntity.getCheckModelData()){
            pageEntity.setCheckModelData(new LoginRecord());
        }
        try {
            List<LoginRecord> loginRecords = logService.getLoginRecord(pageEntity);

            if(pageEntity.getPage()){
                pageEntity.setPage(false);
                pageEntity.setCount(true);
                List<LoginRecord> loginRecords2 = logService.getLoginRecord(pageEntity);
                baseResponse.totalCount = loginRecords2.size() > 0 ? loginRecords2.get(0).getId() : 0;
            }else{
                baseResponse.totalCount = loginRecords.size();
            }
            baseResponse.pageNo = pageEntity.getPageIndex();
            baseResponse.pageSize = pageEntity.getPageSize();
            baseResponse.IsSuccess = true;
            baseResponse.Message = "查询成功";
            baseResponse.data = loginRecords;
        }catch (Exception e){
            baseResponse.IsSuccess = false;
            baseResponse.Message = "查询失败";
            baseResponse.Exception = e.getMessage();
        }
        return baseResponse;
    }

    @PostMapping("/getLogRecord")
    public BaseResponse getLogRecord(@RequestBody PageEntity<LogRecord> pageEntity) {
        BaseResponse baseResponse = new BaseResponse();
        if(null == pageEntity.getCheckModelData()){
            pageEntity.setCheckModelData(new LogRecord());
        }
        try {
            List<LogRecord> logRecords = logService.getLogRecord(pageEntity);

            if(pageEntity.getPage()){
                pageEntity.setPage(false);
                pageEntity.setCount(true);
                List<LogRecord> logRecords2 = logService.getLogRecord(pageEntity);
                baseResponse.totalCount = logRecords2.size() > 0 ? logRecords2.get(0).getId() : 0;
            }else{
                baseResponse.totalCount = logRecords.size();
            }
            baseResponse.pageNo = pageEntity.getPageIndex();
            baseResponse.pageSize = pageEntity.getPageSize();
            baseResponse.IsSuccess = true;
            baseResponse.Message = "查询成功";
            baseResponse.data = logRecords;
        }catch (Exception e){
            baseResponse.IsSuccess = false;
            baseResponse.Message = "查询失败";
            baseResponse.Exception = e.getMessage();
        }
        return baseResponse;
    }
    @PostMapping("/getSystemLog")
    public BaseResponse getSystemLog(@RequestBody PageEntity<HashMap> pageEntity) {
        BaseResponse baseResponse = new BaseResponse();
        if(null == pageEntity.getCheckModelData()){
            pageEntity.setCheckModelData(new HashMap());
        }
        try {
            HashMap map = pageEntity.getCheckModelData();

            BaseResponse logRecords = mongoDBRead.getlogs(
                    pageEntity.getPageIndex(),
                    pageEntity.getPageSize(),
                    GetMapValue(map,"traceid"),
                    GetMapValue(map,"ip"),
                    GetMapValue(map,"service"),
                    GetMapValue(map,"level"),
                    GetMapValue(map,"message"),
                    GetMapValue(map,"errorinfo"),
                    GetMapValue(map,"dtbeg"),
                    GetMapValue(map,"dtend"),
                    GetMapValue(map,"pid"));

            baseResponse.pageNo = pageEntity.getPageIndex();
            baseResponse.pageSize = pageEntity.getPageSize();
            baseResponse.IsSuccess = true;
            baseResponse.Message = "查询成功";
            baseResponse.data = logRecords.data;
            baseResponse.totalCount = logRecords.totalCount;
        }catch (Exception e){
            baseResponse.IsSuccess = false;
            baseResponse.Message = "查询失败";
            baseResponse.Exception = e.toString();
        }
        return baseResponse;
    }

    private String GetMapValue(HashMap map, String key)
    {
        if(!map.containsKey(key) || map.get(key) == null)
            return null;
        return map.get(key).toString();
    }
}
